var app_login = angular.module("myApp");

app_login.controller("controller_regist", function($scope, $rootScope, httpRequest, $location) {
    var reg = new RegExp("[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？ ]"); // 特殊字符正则

    $scope.vcodeTip = '获取验证码';
    $scope.reSendSec = 60;
    $scope.reSendTimer = null;
    $scope.isSend = true; // 是否可以发送验证码
    $scope.registview = true;
    $scope.seladdressView = false;

    $scope.regist_tel = ''; // 注册用手机号
    $scope.regist_code = ''; // 注册用验证码
    $scope.regist_pwd = ''; // 注册用密码
    $scope.regist_name = ''; // 注册用名字
    $scope.regist_idcard = ''; // 注册用身份证
    $scope.regist_province_code = ''; // 注册用省份
    $scope.regist_province_name = '请选择地区'; // 注册用省份
    $scope.regist_city_code = ''; // 注册用城市
    $scope.regist_city_name = ''; // 注册用城市
    $scope.regist_country_code = ''; // 注册用区县
    $scope.regist_country_name = ''; // 注册用区县
    $scope.regist_address = ''; // 注册用地址

    /**
     * 发送验证码
     */
    $scope.sendCode = function() {
        if ($scope.isSend) {
            var validatePhone = $scope.validate_phone($scope.regist_tel);
            // 前端校验手机号
            if ($scope.regist_tel == '') {
                $.dialog.showToast({ msgTxt: '请输入手机号。' });
                return;
            } else if (!validatePhone.flag) {
                $.dialog.showToast({ msgTxt: validatePhone.msg });
                return;
            }

            var data = {
                success: true,
                msg: '发送成功！',
                obj: 4322
            };
            if (data.success === true) { // 发送成功
                // 发送验证码按钮 变更
                $scope.isSend = false;
                $scope.vcodeTip = $scope.reSendSec + '秒后可重发';
                $scope.timerStart();

                $scope.sys_code = data.obj;
            } else if (data.success === false) { // 发送失败
                $.dialog.showToast({
                    msgTxt: data.msg,
                    callbackOk: function() {
                        $scope.sendVCodeBtnInit();
                    }
                });
            }
        }
    };

    /**
     * 手机号校验
     */
    $scope.validate_phone = function(phontVal) {
        var regPhone = /^1[34578]\d{9}$/;
        var result = { flag: false, msg: '' };
        if (!regPhone.test(phontVal) || phontVal.length < 11 || phontVal.length > 11) {
            result.flag = false;
            result.msg = '请输入正确的手机号。';
        } else {
            result.flag = true;
            result.msg = '';
        }
        return result;
    };

    /**
     * *秒后可重发验证码 计时器
     */
    $scope.timerStart = function() {
        $scope.reSendTimer = window.setInterval(function() {
            if ($scope.reSendSec == 0) {
                $scope.sendVCodeBtnInit();
            } else {
                $scope.reSendSec--;
                document.getElementById('btnVCodeGet').innerHTML = $scope.reSendSec + '秒后可重发';
            }
        }, 1000);
    };

    /**
     * 发送验证码按钮初始化
     */
    $scope.sendVCodeBtnInit = function() {
        clearInterval($scope.reSendTimer);
        $scope.reSendSec = 60;
        $scope.isSend = true;
        $scope.vcodeTip = '获取验证码';
        document.getElementById('btnVCodeGet').innerHTML = $scope.vcodeTip;
    };

    /**
     * 名字校验
     */
    $scope.validate_name = function(val) {
        var result = { flag: true, msg: '' };

        if (reg_specialchar.test(val)) {
            result.flag = false;
            result.msg = "您的名字里不能包含" + tip_specialchar + "能特殊字符。";
        } else if (val.length > 20) {
            result.flag = false;
            result.msg = '您的名字长度不能超过20位。';
        }

        return result;
    };

    /**
     * 密码校验
     */
    $scope.validate_pwd = function(val) {
        var result = { flag: true, msg: '' };

        if (reg_specialchar.test(val)) {
            result.flag = false;
            result.msg = "您的密码里不能包含" + tip_specialchar + "能特殊字符。";
        } else if (val.length > 32) {
            result.flag = false;
            result.msg = '您的密码长度不能超过32位。';
        }

        return result;
    };

    /**
     * 身份证校验
     */
    $scope.validate_idcard = function(val) {
        var result = { flag: true, msg: '' };

        if (reg_specialchar.test(val)) {
            result.flag = false;
            result.msg = "您的身份证里不能包含" + tip_specialchar + "能特殊字符。";
        } else if (val.length != 15 && val.length != 18) {
            result.flag = false;
            result.msg = '身份证长度只能为15位或者18位。';
        }

        return result;
    };

    /**
     * 地址校验
     */
    $scope.validate_address = function(val) {
        var result = { flag: true, msg: '' };

        if (reg_specialchar.test(val)) {
            result.flag = false;
            result.msg = "您的地址里不能包含" + tip_specialchar + "能特殊字符。";
        }
        if (val.length > 30) {
            result.flag = false;
            result.msg = '您的地址长度不能超过30位。';
        }

        return result;
    };

    /**
     * 注册按钮 点击
     */
    $scope.fnRegist = function() {
        if ($scope.regist_tel != '' && $scope.regist_code != '' && $scope.regist_pwd != '') {
            // 校验
            var validatePhone = $scope.validate_phone($scope.regist_tel);
            if (!validatePhone.flag) {
                $.dialog.showToast({
                    msgTxt: validatePhone.msg,
                    callbackOk: function() {
                        document.getElementById("regist_tel").focus();
                    }
                });
                return;
            }

            var validatePwd = $scope.validate_pwd($scope.regist_pwd);
            if (!validatePwd.flag) {
                $.dialog.showToast({
                    msgTxt: validatePwd.msg,
                    callbackOk: function() {
                        document.getElementById("regist_pwd").focus();
                    }
                });
                return;
            }

            var registName = $scope.validate_name($scope.regist_name);
            if (!registName.flag) {
                $.dialog.showToast({
                    msgTxt: registName.msg,
                    callbackOk: function() {
                        document.getElementById("regist_name").focus();
                    }
                });
                return;
            }

            if ($scope.regist_idcard != '') {
                var validateIdcard = $scope.validate_idcard($scope.regist_idcard);
                if (!validateIdcard.flag) {
                    $.dialog.showToast({
                        msgTxt: validateIdcard.msg,
                        callbackOk: function() {
                            document.getElementById("regist_idcard").focus();
                        }
                    });
                    return;
                }
            }

            var validateAddress = $scope.validate_address($scope.regist_address);
            if (!validateAddress.flag) {
                $.dialog.showToast({
                    msgTxt: validateAddress.msg,
                    callbackOk: function() {
                        document.getElementById("regist_address").focus();
                    }
                });
                return;
            }


            var data = {
                success: true,
                msg: '注册成功！'
            };
            if (data.success === true) {
                $.dialog.showToast({
                    msgTxt: '注册成功。',
                    callbackOk: function() {
                        // 保存家属个人信息到sessionStorage
                        data_sessionStorage = JSON.parse(sessionStorage.getItem('data_sessionStorage'));
                        data_sessionStorage.familyInfo.phone = $scope.regist_tel;
                        sessionStorage.setItem('data_sessionStorage', JSON.stringify(data_sessionStorage));

                        // 跳转到首页
                        window.location.replace(pagePath + 'familyapp.html#/home');
                    }
                });
            } else if (data.success === false) {
                $.dialog.showToast({ msgTxt: data.msg });
            }

        }
    };

    // ============== 选择省份城市区县
    $scope.registSelCountry = function() {
        $scope.registview = false;
        $scope.seladdressView = true;
        $scope.chooseProvince = true;
    	var data = {
    		obj: [{
    			code: '0',
    			name: '江苏',
    			child: [{
    				code: '7',
    				name: '南京'
    			}, {
    				code: '7',
    				name: '无锡'
    			}, {
    				code: '7',
    				name: '常州'
    			}, {
    				code: '7',
    				name: '苏州'
    			}]
    		}, {
    			code: '1',
    			name: '安徽',
    			child: [{
    				code: '7',
    				name: '合肥'
    			}, {
    				code: '7',
    				name: '芜湖'
    			}, {
    				code: '7',
    				name: '蚌埠'
    			}, {
    				code: '7',
    				name: '马鞍山'
    			}]
    		}, {
    			code: '2',
    			name: '湖南',
    			child: [{
    				code: '7',
    				name: '长沙'
    			}, {
    				code: '7',
    				name: '株洲'
    			}, {
    				code: '7',
    				name: '湘潭'
    			}, {
    				code: '7',
    				name: '衡阳'
    			}]
    		}, {
    			code: '3',
    			name: '湖北',
    			child: [{
    				code: '7',
    				name: '武汉'
    			}, {
    				code: '7',
    				name: '黄石'
    			}, {
    				code: '7',
    				name: '十堰'
    			}, {
    				code: '7',
    				name: '荆州'
    			}]
    		}, {
    			code: '4',
    			name: '河南',
    			child: [{
    				code: '7',
    				name: '郑州'
    			}, {
    				code: '7',
    				name: '开封'
    			}, {
    				code: '7',
    				name: '洛阳'
    			}, {
    				code: '7',
    				name: '安阳'
    			}]
    		}, {
    			code: '5',
    			name: '河北',
    			child: [{
    				code: '7',
    				name: '石家庄'
    			}, {
    				code: '7',
    				name: '唐山'
    			}, {
    				code: '7',
    				name: '秦皇岛'
    			}, {
    				code: '7',
    				name: '邯郸'
    			}]
    		}, {
    			code: '6',
    			name: '山东',
    			child: [{
    				code: '7',
    				name: ''
    			}, {
    				code: '7',
    				name: '济南'
    			}, {
    				code: '7',
    				name: '青岛'
    			}, {
    				code: '7',
    				name: '淄博'
    			}]
    		}]
    	};
        $scope.provinceList = data.obj;
    };

    /**
     * 省份点击事件
     */
    $scope.provinceClick = function(index, code, name) {
        $scope.chooseCity = true; // 显示城市
        $scope.chooseProvince = false; // 隐藏省份
        $scope.citys = $scope.provinceList[index].child;

        $scope.regist_province_code = code; // 注册用省份
        $scope.regist_province_name = name; // 注册用省份
    };

    /**
     * 城市点击事件
     */
    $scope.cityClick = function(code, name) {
       
        	var data = {
        		obj : [{
        			code: '1',
        			name: '江宁'
        		}, {
        			code: '1',
        			name: '秦淮'
        		}, {
        			code: '1',
        			name: '雨花台'
        		}, {
        			code: '1',
        			name: '鼓楼'
        		}, {
        			code: '1',
        			name: '建邺'
        		}, {
        			code: '1',
        			name: '玄武'
        		}, {
        			code: '1',
        			name: '浦口'
        		}, {
        			code: '1',
        			name: '六合'
        		}]
        	};
            $scope.chooseCountry = true; // 显示区域
            $scope.chooseCity = false; // 隐藏城市
            $scope.chooseProvince = false; // 隐藏省份
            $scope.countrys = data.obj;

            $scope.regist_city_code = code; // 注册用城市
            $scope.regist_city_name = name; // 注册用城市
       
    };

    /**
     * 区县点击事件
     */
    $scope.countryClick = function(code, name) {
        $scope.registview = true;
        $scope.seladdressView = false;
        $scope.regist_country_code = code; // 注册用区县
        $scope.regist_country_name = name; // 注册用区县
    };
});